<?php
namespace app\index\controller;
use app\index\controller\Home;
use think\Request;
use think\Session;

class Evaluate extends Home
{
    /**
     * 给某条评论点赞
     */
    public function zan ()
    {
        if(empty(session("home"))){
            echo 0;
            die;
        }

        $card_id = Request::instance()->param("id");//帖子id
        $eval_id = Request::instance()->param("evalId");//评论id
//        echo $eval_id;die;
        $user_id = session("home.id");
        $data['card_id'] = $eval_id;
        $data['user_id'] = $user_id;

        $zan = model("zan");
        $result = $zan -> queryZan($data);
//        echo $result;
        $eval = model("evaluate");
        // 如果返回值打于0 则已经点过赞了
        if($result > 0){
            // 把这条记录删除掉
            $zan -> deleteZan($data);
            // 评论表中的该条评论数减1
            $eval ->setDecEval("id=$eval_id","zan",1);
            echo 2;
        }else{
            //添加数据到点赞表
            $zan -> addZan($data);
            // 评论表中的该条评论数加1
            $eval ->setIncEval("id=$eval_id","zan",1);
            echo 1;
        }

    }

    /**
     * 提问贴的采纳
     */
    public function take ()
    {
        // 判断用户是否登录
        if(empty(session('home'))){
            echo 0;
            die;
        }

        //获取发帖人的id
        $user_id = Request::instance()->param("id");
        //帖子的id
        $card_id = Request::instance()->param("card_id");
        //这条评论的id
        $eval_id = Request::instance()->param("eval_id");
        //发这条评论人的id
        $eval_user_id = Request::instance()->param("eval_user_id");

        //获取当前登录人的id
        $id = session("home.id");
        if($id != $user_id){
            echo 0;
            die;
        }

        //把该帖子的good_user_id 改为该评论人的id
        $card = model("card");
        $result = $card ->updateCard(['id'=>$card_id],['good_user_id'=>$eval_user_id,"is_end"=>2]);

        if($result > 0){
            //把该条评论的take 改为1
            $eval = model("evaluate");
            $result = $eval ->updateEval(['id'=>$eval_id],['take'=>'1']);
            if($result > 0){
                //再把该提问帖的酬金 给发这条评论的人
                //查看该提问帖的酬金
                $cardinfo = $card->queryCardByid($card_id);
                $gold = $cardinfo['gold'];
                $user = model("user");
                $user ->setIncUser("id=$eval_user_id","gold",$gold);
                echo 2;
            }else{
                $card ->updateCard(['id'=>$card_id],['good_user_id'=>'',"is_end"=>1]);
                echo 1;
            }
        }else{
            echo 1;
        }

    }
}
?>